package com.authine.cloudpivot.ext.vo.zhende.update;
import com.authine.cloudpivot.ext.vo.zhende.PushOrderUpdateModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "header",
    "body"
})
@XmlRootElement(name = "soapenv:Envelope")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class EnvelopeUpdate {

    @XmlAttribute(name="xmlns:soapenv")
    private String soapenv="http://schemas.xmlsoap.org/soap/envelope/";
    @XmlAttribute(name="xmlns:s4")
    private String eapp="http://ZDSAP.COM/S4";

    @XmlElement(name = "soapenv:Header")
    protected Header header;
    @XmlElement(name = "soapenv:Body")
    protected com.authine.cloudpivot.ext.vo.zhende.update.Body body;

    public EnvelopeUpdate(Header header, com.authine.cloudpivot.ext.vo.zhende.update.Body body) {
        this.header = header;
        this.body = body;
    }

    public static EnvelopeUpdate toXml(List<PushOrderUpdateModel> pushOrderUpdateModelList) {
        List<TDATA> tdataList = new ArrayList<>();
        List<TDATAX> tdataxList = new ArrayList<>();
        List<TSALEDATA> tsaledataList = new ArrayList<>();
        for(PushOrderUpdateModel orderGoods : pushOrderUpdateModelList){
            TDATA tdata = TDATA.builder()
                    .matnr(orderGoods.getItemNumber())
                    .zzkpgg(orderGoods.getInvoicingSpecifications())
                    .zzggxh(orderGoods.getSpecificationModel())
                    .zzpinp(orderGoods.getBrand())
                    .zkpmc(orderGoods.getProductName())
                    .build();
            tdataList.add(tdata);
            TDATAX tdatax = TDATAX.builder()
                    .matnr(orderGoods.getItemNumber())
                    .zzkpgg("X")
                    .zzggxh("X")
                    .zzpinp("X")
                    .zkpmc("X")
                    .build();
            tdataxList.add(tdatax);
            TSALEDATA tsaledata = TSALEDATA.builder()
                    .zmatnr(orderGoods.getItemNumber())
                    .zkpmc(orderGoods.getProductName())
                    .build();
            tsaledataList.add(tsaledata);
        }
        return new EnvelopeUpdate(new Header(), com.authine.cloudpivot.ext.vo.zhende.update.Body.builder()
                                .mt0010MATERIALReq(com.authine.cloudpivot.ext.vo.zhende.update.MT0010MATERIALReq.builder()
                                .tdata(tdataList)
                                .tdatax(tdataxList)
                                .tsaledata(tsaledataList)
                                .build())
                        .build()
        );
    }
}
